package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    /**
     * URL: /file/upload
     * 类型: post
     * 参数: file=字节信息
     * 返回值: SysResult(ImageVO)
     * 知识点:
     *   SpringMVC针对与IO操作开发了MultipartFile
     *   底层实现就是常规IO流,简化了用户的操作过程.无需手动关流
     *   SpringMVC中默认支持最大1M
     * 步骤:
     *      1.获取文件名称
     *      2.准备文件路径
     *      3.准备文件上传的全路径
     *      4.实现文件上传操作
     */
    @Autowired
    private FileService fileService;
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file) {
        ImageVO imageVO = fileService.upload(file);
        if (imageVO==null){
            return SysResult.fail();
        }
            return SysResult.success(imageVO);

    }
    @DeleteMapping("/deleteFile")
    @Transactional
    public SysResult deleteFile(String virtualPath){
        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }

    }





